<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        h2 {
            height: 40px;
            line-height: 40px;
            text-indent: 10px;
        }

        .list li {
            height: 40px;
            line-height: 40px;
            border: 1px dashed #333;
            text-indent: 10px;
        }

        .list li+li {
            border-top: 0;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h2 id="title">批量操作元素</h2>
        <ul class="list">
            <li>01</li>
            <li>02</li>
            <li>03</li>
            <li>04</li>
            <li>05</li>
            <li>06</li>
        </ul>


    </div>

    <div class="button-groups">
        <button class="setText">设置text</button>
        <button class="getText">获取text</button>
    </div>
</body>
<script>
    function GetEle(selector) {
        var list = document.querySelectorAll(selector); // css选择器

        list = Array.from(list);
        Object.setPrototypeOf(list, GetEle.prototype);

        return list;
    }

    GetEle.prototype.each = function (callbackFn) {
        // this -> 调用此方法的实例化对象(伪数组)
        for (var i = 0; i < this.length; i++) {
            var item = this[i];
            // callbackFn(item, i, this);
            callbackFn.call(item, item, i, this);
        }
    }

    GetEle.prototype.html = function (html) {
        // this -> 调用此方法的实例化对象(伪数组) -> 可以通过原型属性 访问构造函数的原型对象上公用属性和方法
        if (arguments.length >= 1) {
            this.each(function (ele, i) {
                ele.innerHTML = html;
            })
        } else {
            return this[0].innerHTML;
        }
    }

    function $(selector) {
        return new GetEle(selector);
    }

    // var liObj = new GetEle(".list li");
    // console.log(liObj);

    // liObj.each(function (item, index, array) {
    //     // console.log(item, index, array);
    //     console.log(this);
    // })

    $(".list li").each(function () {
        console.log(this);
    })

</script>

</html>